/*****************************************************************************

File:   otkAssemblyItemOperationsExample.java

Date          Build     Author        Version    Comments
01-Nov-12    P-20-16    dshah          $$1        Created
20-mar-15 P-30-05 gshmelev $$2  called GetCurrentSession

****************************************************************************/

/* 
   
This example will demonstrate basic operations using 
Assembly Item/Flexible Components.

*/

package com.ptc.otkjavaexamples;

import com.ptc.cipjava.*;
import com.ptc.pfc.pfcSession.*;
import com.ptc.pfc.pfcCommand.*;
import com.ptc.pfc.pfcGlobal.*;
import com.ptc.pfc.pfcModel.*;
import com.ptc.pfc.pfcModelItem.*;
import com.ptc.pfc.pfcFeature.*;
import com.ptc.pfc.pfcSolid.*;
import javax.swing.JOptionPane;
import java.io.*;

import com.ptc.cipjava.*;
import com.ptc.pfc.pfcFamily.*;
import com.ptc.pfc.pfcServer.*;
import com.ptc.pfc.pfcWindow.*;
import com.ptc.wfc.wfcSolid.*;
import com.ptc.pfc.pfcAssembly.*;
import com.ptc.wfc.wfcAssembly.*;
import com.ptc.wfc.wfcComponentFeat.*;
import com.ptc.pfc.pfcComponentFeat.*;

/* ---------------------------------------------------
    * ASSEMBLY STRUCTURE USED FOR THE EXAMPLE *
---------------------------------------------------
							---------		-------
							 FEAT ID 		 FEAT #
							---------		-------
FLEX_TOP_ASM.asm
|
|---ASM_RIGHT						    1  	 		   1
|---ASM_TOP						    3			   2
|---ASM_FRONT						    5			   3
|---ASM_DEF_CSYS					    7			   4
|
|===FLEX_PART2.prt					   39			   5
|
|===FLEX_SUB_ASM1.asm					   42			   6
	|
	|---ASM_RIGHT				    	    1			   1
	|---ASM_TOP					    3			   2
	|---ASM_FRONT					    5			   3
	|---ASM_DEF_CSYS				    7			   4
	|
	|===FLEX_PART1.prt				   39			   5
	|
	|===FLEX_SUB_ASM2.asm				   40			   6
		|
		|---ASM_RIGHT				    1			   1
		|---ASM_TOP				    3			   2
		|---ASM_FRONT				    5			   3
		|---ASM_DEF_CSYS			    7			   4
		|
		|===FLEX_PART1.prt			   39			   5
		|===FLEX_PART2.prt			   40			   5
			
---------------------------------------------------
    * ASSEMBLY STRUCTURE USED FOR THE EXAMPLE *
--------------------------------------------------- */


//***********************************************************

class otkAssemblyItemOperationsExample extends DefaultUICommandActionListener
{
    
    
    
    public otkAssemblyItemOperationsExample()
    {
    }
    
    
    
    
    public void OnCommand()
    {
	addAssemblyItemOperationsExample();
    }
    
    
    public static void addAssemblyItemOperationsExample ()
    {
	try
	    {			
		FileWriter file = new FileWriter("AssemblyItemExample.txt");
		BufferedWriter fp_out_asmitem = new BufferedWriter (file);
		
		fp_out_asmitem.write("- Assembly Item Example Example -");
		fp_out_asmitem.newLine();
		fp_out_asmitem.write("==============================");
		fp_out_asmitem.newLine();
		
		Session session = pfcSession.GetCurrentSession ();
      
		/* Open top assembly FLEX_TOP_ASM.asm in Creo Session*/
		Model CurrModel = session.GetCurrentModel();
      
		Assembly PAsm = (Assembly)(CurrModel);
      
		WAssembly WAsm = (WAssembly)(CurrModel);
      
		ModelDescriptor Part_MDesc = pfcModel.ModelDescriptor_CreateFromFileName("flex_part2.prt");
		Model PartOwner = session.GetModelFromDescr(Part_MDesc);
      
		/* Populate component Ids for Component Path */
		intseq int_id_array = intseq.create();
		int_id_array.set(0, 40);
      
		/* Create Component Path with respect to top assembly*/
		ComponentPath AsmCompPath = pfcAssembly.CreateComponentPath (PAsm, int_id_array);
		WComponentPath WAsmCompPath = (WComponentPath)(AsmCompPath);
      
		AssemblyItems AsmItemArray = AssemblyItems.create();
		
		/* Populate dimension Ids (from flex_part2.prt) for AssemblyItem array */
		intseq dim_id_array = intseq.create();
		dim_id_array.set(0, 0);
		dim_id_array.set(1, 1);
		dim_id_array.set(2, 2);
		
		/* Populate AssemblyItem array from Dimensions */
		AsmItemArray = otkAssemblyItemUtils.OTKUtilPopulateAssemblyItemsDimensionArray(
											       AsmItemArray, WAsm, PartOwner, dim_id_array, WAsmCompPath, fp_out_asmitem);
      
		
		ModelDescriptor Asm_MDesc = pfcModel.ModelDescriptor_CreateFromFileName("flex_sub_asm1.asm");
		Model AsmOwner = session.GetModelFromDescr(Asm_MDesc);
		Solid asmSolid = (Solid)(AsmOwner);
		
		/*Set Component Flexible using AssemblyItem Array*/
		WComponentFeat WCFeat = otkAssemblyItemUtils.OTKUtilSetFlexibleComponent(asmSolid, 40, AsmItemArray, fp_out_asmitem);
		
		int_id_array.set(0, 40);
		int_id_array.set(1, 39);
		
		/* Create Component Path with respect to sub assembly */
		Assembly PAsm_flex1 = (Assembly)(AsmOwner);
		ComponentPath AsmCompPath2 = pfcAssembly.CreateComponentPath (PAsm_flex1, int_id_array);
		WComponentPath WAsmCompPath2 = (WComponentPath)(AsmCompPath2);
		
		/* Create Flexible Model using AssemblyItem Array */
		Model FlexModel = otkAssemblyItemUtils.OTKUtilCreateFlexibleModel(asmSolid, 40, WAsmCompPath2, AsmItemArray, fp_out_asmitem);
		
		fp_out_asmitem.write("Unsetting Component Flexible..."); 
		fp_out_asmitem.newLine();
		
		/* Unset component flexible */
		WCFeat.UnsetAsFlexible();
		
		boolean is_flex = WCFeat.IsFlexible();
		
		fp_out_asmitem.write(" Is Component Flexible ? : " + (is_flex ? "YES" : "NO"));
		fp_out_asmitem.newLine();
		
		AssemblyItems AsmItemArray2 = AssemblyItems.create();
		
		/* Populate dimension Ids (from flex_part2.prt) for AssemblyItem array */
		intseq dim_id_array2 = intseq.create();
		dim_id_array2.set(0, 0);
		dim_id_array2.set(1, 1);
		
		/* Populate AssemblyItem array from Dimensions */
		AsmItemArray2 = otkAssemblyItemUtils.OTKUtilPopulateAssemblyItemsDimensionArray(
												AsmItemArray2, WAsm, PartOwner, dim_id_array2, null, fp_out_asmitem);
		
		/* Populate feature Ids (from flex_part2.prt) for AssemblyItem array */
		intseq feat_id_array = intseq.create();
		feat_id_array.set(0, 39);
      
		/* Populate AssemblyItem array from Features */
		AsmItemArray2 = otkAssemblyItemUtils.OTKUtilPopulateAssemblyItemsFeatureArray(
											      AsmItemArray2, WAsm, PartOwner, feat_id_array, null, fp_out_asmitem);
		
		Solid pSolid = (Solid)(CurrModel);
		
		/*Create Predefined Flexibility Component using AssemblyItem Array*/
		otkAssemblyItemUtils.OTKUtilCreatePredefinedFlexibilityComponent(pSolid, 39, AsmItemArray2, PartOwner, fp_out_asmitem);		
		fp_out_asmitem.close();
		return;
	    }
	catch (Exception e)
	    {
		System.out.println("1: " + e);
	    }
	return;
    }
    
};






